﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace TravelexForwardContract
{
    class XDatabaseOperation 
    {
        private String m_connectionString;
        private String m_table_name;
        private XLog m_xLog;

        public static int GetLineNum()
        {
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
            return st.GetFrame(0).GetFileLineNumber();
        }

        public XDatabaseOperation()
        {
            m_connectionString =
                "Data Source=HEEFAN-PC\\SQLEXPRESS;" +
                "Initial Catalog=TravelexForwardContract;" +
                "Integrated Security=true";
            m_table_name = "allrecords";
            m_xLog = new XLog();
        }

      
        public DataSet getSelectedKeywordsFromAllRecords(String column_in_table, String customDataTableName)
        {
            if (column_in_table == null || column_in_table == "")
                throw new Exception("[Fatal Error] Pass in combo box keyword is empty");

            column_in_table.Trim();
            DataSet dataset = new DataSet();
            using (SqlConnection cn = new SqlConnection(m_connectionString))
            {
                try
                {
                    cn.Open();
                }
                catch (System.Exception ex)
                {
                    m_xLog.logMsg("XDatabase:" + GetLineNum() + ":" + ex.Message);
                    throw new InvalidOperationException(ex.Message);
                }
                
                String sqlFindCompanyName = "SELECT " + column_in_table.ToString() + " FROM " + m_table_name.ToString();
                using (SqlCommand cmd = new SqlCommand(sqlFindCompanyName, cn))
                {
                    try
                    {
                        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlFindCompanyName,cn))
                        {
                            dataAdapter.Fill(dataset, customDataTableName.ToString());
                            cn.Close();
                        }
                    }
                    catch (System.Exception ex)
                    {
                        m_xLog.logMsg("XDatabase:" + GetLineNum() + ":" + ex.Message);
                    }
                }

            }

            return dataset;
        }  //end-of-getSelectedKeywordsFromAllRecords

        public void testDb(String serverName)
        {
            serverName = serverName + ";" +
                "Initial Catalog=TravelexForwardContract;" +
                "Integrated Security=true";
            using (SqlConnection cn = new SqlConnection())
            {
                try
                {
                    cn.Open();
                }
                catch (System.Exception ex)
                {
                    m_xLog.logMsg("XDatabase:" + GetLineNum() + ":" + ex.Message);
                    throw new InvalidOperationException(ex.Message);
                }
            }
        }
    }
}
